﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace L.M.System.Application.Services
{
    public class Delivery_addressServices : BaseServices<delivery_address, string>, IDelivery_addressServices
    {
        /// <summary>
        /// 交付地址
        /// </summary>
        private readonly IDelivery_addressRepository delivery_AddressRepository;
        private readonly IStaff_tableServices staff_TableServices;

        public Delivery_addressServices(IDelivery_addressRepository delivery_AddressRepository, IStaff_tableServices staff_TableServices) : base(delivery_AddressRepository)
        {
            this.delivery_AddressRepository = delivery_AddressRepository;
            this.staff_TableServices = staff_TableServices;
        }

        /// <summary>
        /// 交付地址查询 + 分页
        /// </summary>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="tName">查询条件</param>
        /// <returns>交付地址查询 + 分页</returns>
        public async Task<ApiReturn<PageETO<delivery_address>>> GetDelivery_addressRead(int pageSize, int pageIndex, string? tName)
        {
            ApiReturn<PageETO<delivery_address>> apiReturn = new ApiReturn<PageETO<delivery_address>>();
            Expression<Func<delivery_address, bool>> i = c => true;
            if (!string.IsNullOrEmpty(tName))
            {
                i = c => c.address_delivery.Contains(tName);
            }
            var list = await delivery_AddressRepository.GetShowInforAsyncOrder(pageSize, pageIndex, i, c => c.address_operator_date);
            apiReturn.ObjInfor = list;
            return apiReturn;
        }

        /// <summary>
        /// 地址添加
        /// </summary>
        /// <param name="addAdderInforDTO">DTO</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> AddAdderInfor(AddAdderInforDTO addAdderInforDTO)
        {

            try
            {
                //开启事务
                delivery_AddressRepository.BeginTranInfor();

                var id = YitIdHelper.NextId().ToString();
                //修改默认地址
                if (addAdderInforDTO.address_status)
                {
                    var list = await delivery_AddressRepository.GetShowInforAsyncOrder(c => c.address_number != id);
                    foreach (var item in list)
                    {
                        if (item.address_status)
                        {
                            item.address_status = false;
                            break;
                        }
                    }
                    await delivery_AddressRepository.UpdInfor_Batch_Async(list);
                }

                ApiReturn<string> apiReturn = new ApiReturn<string>();
                if (addAdderInforDTO.address_delivery == "")
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "请输入地址";
                    return apiReturn;
                }
                if (addAdderInforDTO.staffId == "")
                {
                    apiReturn.Code = 500;
                    apiReturn.Message = "请先登录";
                    return apiReturn;
                }
                var staName = await staff_TableServices.GetShowObjAsync(c => c.employee_number == addAdderInforDTO.staffId);
                if (staName == null)
                {
                    apiReturn.Code = 500;
                    apiReturn.Message = "请先登录";
                    return apiReturn;
                }
                delivery_address delivery_Address = new delivery_address();
                delivery_Address.address_number = id;
                delivery_Address.address_delivery = addAdderInforDTO.address_delivery;
                delivery_Address.address_operator = staName.employee_name;
                delivery_Address.address_operator_date = DateTime.Now;
                delivery_Address.address_status = addAdderInforDTO.address_status;
                var ges = await delivery_AddressRepository.AddInfor_Async(delivery_Address);



                delivery_AddressRepository.CommitTranInfor();
                if (ges > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加成功";
                    return apiReturn;
                }
                else
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加失败";
                    return apiReturn;
                }

            }
            catch (Exception)
            {
                delivery_AddressRepository.RollbackTranInfor();
                throw;
            }
        }


        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="aId">地址编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> DelInforAdder(string? aId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res = await delivery_AddressRepository.GetShowObjAsync(c => c.address_number == aId);
            if (res == null)
            {
                apiReturn.Code = 500;
                apiReturn.Message = "地址不存在";
                return apiReturn;
            }
            if (res.address_num > 0)
            {
                apiReturn.Code = 500;
                apiReturn.Message = "该地址已有占用";
                return apiReturn;
            }
            var ges = await delivery_AddressRepository.DelInfor_Async(res);
            if (ges > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功";
                return apiReturn;
            }
            return apiReturn;
        }

        /// <summary>
        /// 修改默认地址
        /// </summary>
        /// <param name="rId">编号</param>
        /// <returns>修改默认地址</returns>
        public async Task<ApiReturn<string>> GteUpdSattyInfor(string? rId)
        {

            try
            {
                delivery_AddressRepository.BeginTranInfor();
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                var res = await delivery_AddressRepository.GetShowObjAsync(c => c.address_number == rId);
                if (res != null)
                {
                    var list = await delivery_AddressRepository.GetShowInforAsync();
                    foreach (var item in list)
                    {
                        if (item.address_status)
                        {
                            item.address_status = false;
                            break;
                        }
                    }
                    await delivery_AddressRepository.UpdInfor_Batch_Async(list);
                    res.address_status = true;
                    var gets = await delivery_AddressRepository.UpdInfor_Async(res);
                    if (gets > 0)
                    {
                        delivery_AddressRepository.CommitTranInfor();
                        apiReturn.Code = 200;
                        apiReturn.Message = "修改成功";
                    }
                    else
                    {
                        apiReturn.Code = 500;
                        apiReturn.Message = "修改失败";
                    }
                }
                return apiReturn;
            }
            catch (Exception)
            {
                delivery_AddressRepository.CommitTranInfor();
                throw;
            }
        }
    }
}
